package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsGroupon;
import com.qf.service.DtsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class DtsCouponServiceImpl implements DtsCouponService {
    @Autowired
    DtsCouponMapper couponMapper;

    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, String name, Integer type, Integer status, String sort, String order) {

        IPage<DtsCoupon> ipage = new Page<>(page, limit);
        QueryWrapper<DtsCoupon> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            wrapper.eq("name",name);
        }
        if (!StringUtils.isEmpty(type)) {
            wrapper.eq("type",type);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status",status);
        }
        if ("desc".equals(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        couponMapper.selectPage(ipage,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("items", ipage.getRecords());
        data.put("total", ipage.getTotal());

        return data;

    }

    @Override
    public DtsCoupon create(DtsCoupon coupon) {
        coupon.setAddTime(new Date());
        coupon.setUpdateTime(new Date());
        coupon.setDeleted(false);
        couponMapper.insert(coupon);
        return coupon;
    }

    @Override
    public DtsCoupon findById(Integer id) {
        DtsCoupon dtsCoupon = couponMapper.selectById(id);
        return dtsCoupon;
    }

    @Override
    public DtsCoupon update(DtsCoupon coupon) {
        coupon.setUpdateTime(new Date());
        couponMapper.updateById(coupon);
        return coupon;
    }

    @Override
    public void delete(DtsCoupon coupon) {
        couponMapper.deleteById(coupon.getId());
    }
}